package com.relytosoft.mizar.media.analyze.services.service.face.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.exception.ServiceException;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.analyze.api.entity.face.AyFaceTask;
import com.relytosoft.mizar.media.analyze.api.entity.face.AyFaceTaskSon;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyFaceTaskDto;
import com.relytosoft.mizar.media.analyze.api.pojo.query.AyFaceTaskQuery;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyCameraVo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyFaceTaskVo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyTaskSelectVo;
import com.relytosoft.mizar.media.analyze.services.mapper.face.AyFaceTaskMapper;
import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceTaskService;
import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceTaskSonService;
import com.relytosoft.mizar.media.common.config.properties.MizarAnalyzeProperties;
import com.relytosoft.mizar.media.common.enums.DeleteStatusEnum;
import com.relytosoft.mizar.media.common.enums.TaskStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * AyFaceTaskServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/6/1 18:35
 * @Version 1.0
 */
@Service
public class AyFaceTaskServiceImpl  extends ServiceImpl<AyFaceTaskMapper, AyFaceTask> implements AyFaceTaskService {

    @Resource
    private MizarAnalyzeProperties analyzeProperties;

    @Resource
    private AyFaceTaskSonService ayFaceTaskSonService;

    @Override
    public IPage<AyFaceTaskVo> selectAyFaceTaskPageList(PageQuery pageQuery, AyFaceTaskQuery ayFaceTaskQuery) {
        if (StrUtil.isBlank(pageQuery.getOrderByColumn())) {
            pageQuery.setOrderByColumn("id");
            pageQuery.setIsAsc("desc");
        }
        if(Objects.isNull(ayFaceTaskQuery.getType())){
            ayFaceTaskQuery.setType(1);
        }
        if(StrUtil.isNotEmpty(ayFaceTaskQuery.getEndTime())){
            LocalDateTime endTime=  Convert.toLocalDateTime(ayFaceTaskQuery.getEndTime()).plusDays(1);
            ayFaceTaskQuery.setEndTime(endTime.format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        return baseMapper.selectPageTaskList(pageQuery.build(), ayFaceTaskQuery);
    }
    @Override
    public List<AyFaceTaskSon> buildTaskSon(AyFaceTaskDto ayFaceTaskDto)
    {
        //构造子任务列表
        List<AyFaceTaskSon> ayFaceTaskSonList=new ArrayList<>();
        for (AyCameraVo cameraVo : ayFaceTaskDto.getAyCameraVos()) {
            AyFaceTaskSon ayFaceTaskSon = new AyFaceTaskSon();
            BeanCopyUtils.copy(cameraVo,ayFaceTaskSon);
            ayFaceTaskSon.setTaskName(ayFaceTaskDto.getName());
            ayFaceTaskSon.setStatus(ayFaceTaskDto.getStatus());
            ayFaceTaskSon.setType(ayFaceTaskDto.getType());
            if(ayFaceTaskDto.getStatus()==TaskStatusEnum.CREATED.getValue()){
                ayFaceTaskSon.setBeginTime(LocalDateTime.now());
            }
            ayFaceTaskSonList.add(ayFaceTaskSon);
        }
        return ayFaceTaskSonList;
    }
    @Override
    public void taskStatusHandle(Integer id, Integer newStatus) {
        String updateBy=CurrentUser.getCurrentUserFullName();
        if(StrUtil.isEmpty(updateBy)){
            updateBy="SYSTEM";
        }
        if(newStatus==TaskStatusEnum.CREATED.getValue()){
            //启动成功联动处理主任务状态
            List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.FINISHED.getValue(),TaskStatusEnum.NOT_STARTED.getValue(),TaskStatusEnum.RUNING.getValue()));

            baseMapper.update(null,new LambdaUpdateWrapper<AyFaceTask>().set(AyFaceTask::getStatus,newStatus).set(AyFaceTask::getUpdatedId, CurrentUser.getCurrentUserId()).set(AyFaceTask::getUpdatedBy, updateBy).set(AyFaceTask::getEndTime,null).set(AyFaceTask::getBeginTime,LocalDateTime.now())
                    .eq(AyFaceTask::getId,id).eq(AyFaceTask::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue()).in(AyFaceTask::getStatus,status));
        }
        else if(newStatus==TaskStatusEnum.FINISHED.getValue()){
            updateStatusFinished(id,updateBy);
        }else if(newStatus==TaskStatusEnum.RUNING.getValue()){
            updateStatusRuning(id,updateBy);
        }
    }

    @Override
    public void updateStatusByDeleteUnusual(Integer id) {
        //查询所有未删除的子任务
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AyFaceTaskSon::getFaceTaskId,id);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.CREATED.getValue(),TaskStatusEnum.RUNING.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper.in(AyFaceTaskSon::getStatus,status);
        List<AyFaceTaskSon> taskSonList=ayFaceTaskSonService.list(wrapper);
        if(!taskSonList.isEmpty()){
            //是否还有异常任务
            boolean hasUnusual=taskSonList.stream().anyMatch(s -> s.getStatus()==TaskStatusEnum.UNUSUAL.getValue());
            //是否有运行中的任务
            boolean hasRunning=taskSonList.stream().anyMatch(s -> s.getStatus()==TaskStatusEnum.RUNING.getValue());
            //是否有创建中的任务
            boolean hasCreate=taskSonList.stream().anyMatch(s -> s.getStatus()==TaskStatusEnum.CREATED.getValue());
            if(!hasUnusual){
                //没有异常任务
                if(hasCreate){
                    //有创建中的任务
                    AyFaceTask updateTask=new AyFaceTask();
                    updateTask.setId(id);
                    updateTask.setStatus(TaskStatusEnum.CREATED.getValue());
                    updateTask.setUpdatedBy(CurrentUser.getCurrentUserFullName());
                    this.updateById(updateTask);
                }else if(hasRunning){
                    //有运行中的任务
                    AyFaceTask updateTask=new AyFaceTask();
                    updateTask.setId(id);
                    updateTask.setStatus(TaskStatusEnum.RUNING.getValue());
                    updateTask.setUpdatedBy(CurrentUser.getCurrentUserFullName());
                    this.updateById(updateTask);
                }
            }
        }
    }

    private void updateStatusFinished(Integer id,String updateBy){
        //查询是否还有状态为创建中及运行中且未删除的子任务
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AyFaceTaskSon::getFaceTaskId,id);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.CREATED.getValue(),TaskStatusEnum.RUNING.getValue()));
        wrapper.in(AyFaceTaskSon::getStatus,status);
        List<AyFaceTaskSon> taskSonList=ayFaceTaskSonService.list(wrapper);
        if(taskSonList.isEmpty()){
            AyFaceTask updateTask=new AyFaceTask();
            updateTask.setId(id);
            updateTask.setStatus(TaskStatusEnum.FINISHED.getValue());
            updateTask.setEndTime(LocalDateTime.now());
            this.updateById(updateTask);
        }
        else {
            //是否还有异常任务
            boolean hasUnusual=taskSonList.stream().anyMatch(s -> s.getStatus()==TaskStatusEnum.UNUSUAL.getValue());
            //是否有运行中的任务
            boolean hasRunning=taskSonList.stream().anyMatch(s -> s.getStatus()==TaskStatusEnum.RUNING.getValue());
            if(!hasUnusual&&hasRunning){
                //无异常有运行中
                AyFaceTask updateTask=new AyFaceTask();
                updateTask.setId(id);
                updateTask.setStatus(TaskStatusEnum.RUNING.getValue());
                updateTask.setEndTime(LocalDateTime.now());
                this.updateById(updateTask);
            }
        }
    }
    private void updateStatusRuning(Integer id,String updateBy){
        //查询是否还有状态为创建中或异常且未删除的子任务
        LambdaQueryWrapper<AyFaceTaskSon> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(AyFaceTaskSon::getFaceTaskId,id);
        wrapper1.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.CREATED.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper1.in(AyFaceTaskSon::getStatus,status);
        if(ayFaceTaskSonService.count(wrapper1)==0){
            //创建中的状态变为运行中
            LambdaQueryWrapper<AyFaceTask> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(AyFaceTask::getId,id);
            wrapper.eq(AyFaceTask::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
            wrapper.eq(AyFaceTask::getStatus,TaskStatusEnum.CREATED.getValue());

            AyFaceTask updateTask=new AyFaceTask();
            updateTask.setStatus(TaskStatusEnum.RUNING.getValue());
            updateTask.setUpdatedBy(updateBy);

            this.update(updateTask,wrapper);
        }
    }
    @Override
    public List<AyFaceTask> buildDeleteTask(List<Integer> ids){
        //主任务删除验证
        if (!deleteTaskFrontVerify(ids)) {
            return null;
        }
        //子任务删除验证
        if (!deleteSonTaskFrontVerify(ids)) {
            return null;
        }
        List<AyFaceTask> deleteTaskList=new ArrayList<>();
        for (Integer id : ids) {
            AyFaceTask task=new AyFaceTask();
            task.setId(id);
            task.setIsDeleted(DeleteStatusEnum.DELETED.getValue());
            task.setUpdatedId(CurrentUser.getCurrentUserId());
            task.setUpdatedBy(CurrentUser.getCurrentUserFullName());
            deleteTaskList.add(task);
        }
        return deleteTaskList;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteTaskTrans(List<Integer> ids,List<AyFaceTask> deleteTaskList){
        boolean deleteRes= this.updateBatchById(deleteTaskList,deleteTaskList.size());
        if(deleteRes){
            deleteRes=ayFaceTaskSonService.deleteByTask(ids);
            if(!deleteRes){
                throw new ServiceException("任务删除失败！");
            }
        }
    }

    @Override
    public List<AyFaceTaskSon> selectCanStartTaskSonList(List<Integer> ids) {
        //通过子任务筛选可启动的任务
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTaskSon::getFaceTaskId, ids);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.FINISHED.getValue(),TaskStatusEnum.NOT_STARTED.getValue()));
        wrapper.in(AyFaceTaskSon::getStatus,status);

        return ayFaceTaskSonService.list(wrapper);
    }

    @Override
    public List<AyFaceTaskSon> selectCanStopTaskSonList(List<Integer> ids) {
        //通过子任务筛选可停止的任务
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTaskSon::getFaceTaskId, ids);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        //未删除的运行中、异常、或创建中更新时间已过指定配置的任务
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.RUNING.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper.and(w -> w.in(AyFaceTaskSon::getStatus,status).or(w1->w1.eq(AyFaceTaskSon::getStatus,TaskStatusEnum.CREATED.getValue()).lt(AyFaceTaskSon::getUpdatedTime,LocalDateTime.now().plusSeconds(-1*analyzeProperties.getTaskCanStopTime()))));
        return ayFaceTaskSonService.list(wrapper);
    }

    @Override
    public void updateSonTaskName(Integer id, String taskName) {
        LambdaQueryWrapper<AyFaceTaskSon> wrapper=Wrappers.lambdaQuery();
        wrapper.eq(AyFaceTaskSon::getFaceTaskId,id);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        AyFaceTaskSon ayFaceTaskSon = new AyFaceTaskSon();
        ayFaceTaskSon.setTaskName(taskName);
        ayFaceTaskSonService.update(ayFaceTaskSon, wrapper);
    }

    @Override
    public List<AyTaskSelectVo> selectTaskList() {
        return baseMapper.selectTaskList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertTaskWithSonWith(AyFaceTask ayFaceTask,List<AyFaceTaskSon> ayFaceTaskSonList) {
        boolean save = this.save(ayFaceTask);
        if (save) {
            for (AyFaceTaskSon taskSon:ayFaceTaskSonList){
                taskSon.setFaceTaskId(ayFaceTask.getId());
            }
            boolean saveSon=ayFaceTaskSonService.saveBatch(ayFaceTaskSonList,ayFaceTaskSonList.size());
            if(!saveSon){
                throw new ServiceException("创建结构化任务失败");
            }
            return saveSon;
        }
        throw new ServiceException("创建结构化任务失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean startTaskWithSon(AyFaceTask ayFaceTask,List<AyFaceTaskSon> deleteTaskSons,List<AyFaceTaskSon> insertTaskSons,List<AyFaceTaskSon> startTaskSons){

        List<Integer> status = new ArrayList<>(Arrays.asList(TaskStatusEnum.FINISHED.getValue(), TaskStatusEnum.NOT_STARTED.getValue(), TaskStatusEnum.UNUSUAL.getValue()));

        boolean result = baseMapper.update(null, new LambdaUpdateWrapper<AyFaceTask>()
                .set(AyFaceTask::getStatus, ayFaceTask.getStatus())
                .set(AyFaceTask::getUpdatedId, CurrentUser.getCurrentUserId())
                .set(AyFaceTask::getUpdatedBy, CurrentUser.getCurrentUserFullName())
                .set(AyFaceTask::getUpdatedTime, LocalDateTime.now())
                .set(AyFaceTask::getEndTime, null)
                .set(AyFaceTask::getBeginTime, LocalDateTime.now())

                .eq(AyFaceTask::getId, ayFaceTask.getId())
                .eq(AyFaceTask::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue())
                .in(AyFaceTask::getStatus, status)) == 1;


        if (result) {
            boolean deleteAndStartRes;
            if (!deleteTaskSons.isEmpty()) {
                deleteAndStartRes = ayFaceTaskSonService.deleteAndStartTaskSonList(deleteTaskSons, insertTaskSons, ayFaceTask.getId());
            } else {
                deleteAndStartRes = true;
            }
            boolean startRes;
            if(!startTaskSons.isEmpty()){
                startRes= ayFaceTaskSonService.startTaskSonList(startTaskSons,ayFaceTask.getId());
            }
            else{
                startRes=true;
            }
            if(!deleteAndStartRes||!startRes){
                throw new ServiceException("启动任务失败");
            }
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean stopTaskWithSon(AyFaceTask ayFaceTask,List<AyFaceTaskSon> stopTaskSons){
        boolean result=this.updateById(ayFaceTask);
        if(result) {
            boolean stopRes=ayFaceTaskSonService.stopTaskSonList(stopTaskSons,ayFaceTask.getId());
            if(!stopRes){
                throw new ServiceException("停止任务失败");
            }
        }
        return result;
    }

    /**
     * 任务添加前置验证
     *
     * @param ayFaceTaskDto
     * @return
     */
    @Override
    public Boolean addFrontVerify(AyFaceTaskDto ayFaceTaskDto) {

        List<AyCameraVo> ayCameraVos = ayFaceTaskDto.getAyCameraVos();
        //查询设备是否有未删除的分析任务
        List<String> cameraIds=ayCameraVos.stream().map(AyCameraVo::getCameraId).collect(Collectors.toList());

        List<AyFaceTaskSon> taskSons = ayFaceTaskSonService.list(new LambdaQueryWrapper<AyFaceTaskSon>()
                .in(AyFaceTaskSon::getCameraId, cameraIds)
                .eq(AyFaceTaskSon::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue()));
        if(taskSons.isEmpty()){
            return true;
        }
        Iterator<AyCameraVo> cameraVoIterator = ayCameraVos.iterator();
        while (cameraVoIterator.hasNext()) {
            AyCameraVo cameraVo = cameraVoIterator.next();
            //过滤已经存在的camera
            AyFaceTaskSon taskSon=taskSons.stream().filter(d->d.getCameraId().equals(cameraVo.getCameraId())).findAny().orElse(null);
            if (!Objects.isNull(taskSon)) {
                cameraVoIterator.remove();
            }
        }
        return !ayCameraVos.isEmpty();
    }
    /**
     * 任务删除前置验证
     *
     * @param ids 主任务集合
     * @return false验证不通过 true验证通过
     */
    private Boolean deleteTaskFrontVerify(List<Integer> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        //未删除的已结束、未启动、异常任务
        LambdaQueryWrapper<AyFaceTask> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTask::getId, ids);
        wrapper.eq(AyFaceTask::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.FINISHED.getValue(),TaskStatusEnum.NOT_STARTED.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper.in(AyFaceTask::getStatus,status);
        wrapper.select(AyFaceTask::getId);
        List<Object> canDeleteIds=this.listObjs(wrapper);
        if(canDeleteIds.isEmpty()){
            return false;
        }
        //重新为可删除的任务ID赋值
        ids=(List<Integer>)(List)canDeleteIds;
        return true;
    }

    /**
     * 子任务删除前置验证
     *
     * @param ids 主任务id
     * @return false 验证不通过 true 通过
     */
    private Boolean deleteSonTaskFrontVerify(List<Integer> ids) {
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTaskSon::getFaceTaskId,ids);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        //有运行中、创建中的子任务时，不允许删除
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.CREATED.getValue(),TaskStatusEnum.RUNING.getValue()));
        wrapper.in(AyFaceTaskSon::getStatus,status);
        wrapper.select(AyFaceTaskSon::getFaceTaskId);
        //筛选出不能删除的主任务ID
        List<Object> canNotDeleteTaskSons=ayFaceTaskSonService.listObjs(wrapper);
        if(!canNotDeleteTaskSons.isEmpty()){
            List<Integer> newIds = (List<Integer>)(List)canNotDeleteTaskSons;
            Iterator<Integer> idsIterator=ids.iterator();
            while (idsIterator.hasNext()){
                Integer id=idsIterator.next();
                if(newIds.contains(id)){
                    idsIterator.remove();
                }
            }
        }
        return !ids.isEmpty();
    }

    /**
     * 任务启动前置验证
     *
     * @param ids
     * @return
     */
    @Override
    public boolean startTaskFrontVerify(List<Integer> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        //筛选出未删除的已结束、未启动的任务
        LambdaQueryWrapper<AyFaceTask> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTask::getId, ids);
        wrapper.eq(AyFaceTask::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.FINISHED.getValue(),TaskStatusEnum.NOT_STARTED.getValue()));
        wrapper.in(AyFaceTask::getStatus,status);
        wrapper.select(AyFaceTask::getId);
        List<Object> canStartTaskIds=this.listObjs(wrapper);
        if(canStartTaskIds.isEmpty()){
            return false;
        }
        //子任务启动验证
        return startSonTaskFrontVerify(ids);
    }
    /**
     * 子任务启动前置验证
     *
     * @param ids 主任务id
     * @return false 验证不通过 true 通过
     */
    private Boolean startSonTaskFrontVerify(List<Integer> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        LambdaQueryWrapper<AyFaceTaskSon> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTaskSon::getFaceTaskId,ids);
        wrapper.eq(AyFaceTaskSon::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        //有运行中、创建中、异常的子任务时，不允许启动
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.CREATED.getValue(),TaskStatusEnum.RUNING.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper.in(AyFaceTaskSon::getStatus,status);
        wrapper.select(AyFaceTaskSon::getFaceTaskId);
        //筛选出不能启动的主任务ID
        List<Object> canNotStartTaskSons=ayFaceTaskSonService.listObjs(wrapper);
        if(!canNotStartTaskSons.isEmpty()){
            List<Integer> newIds = (List<Integer>)(List)canNotStartTaskSons;
            Iterator<Integer> idsIterator=ids.iterator();
            while (idsIterator.hasNext()){
                Integer id=idsIterator.next();
                if(newIds.contains(id)){
                    //移出不能删除的主任务ID
                    idsIterator.remove();
                }
            }
        }
        return !ids.isEmpty();
    }
    /**
     * 任务停止前置验证
     *
     * @param ids
     * @return false 验证不通过 true 通过
     */
    @Override
    public boolean stopTaskFrontVerify(List<Integer> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        //未删除的运行中、异常、创建中更新时间已过指定配置的任务
        LambdaQueryWrapper<AyFaceTask> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyFaceTask::getId, ids);
        wrapper.eq(AyFaceTask::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue());
        List<Integer> status=new ArrayList<>(Arrays.asList(TaskStatusEnum.RUNING.getValue(),TaskStatusEnum.UNUSUAL.getValue()));
        wrapper.and(w -> w.in(AyFaceTask::getStatus,status).or(w1->w1.eq(AyFaceTask::getStatus,TaskStatusEnum.CREATED.getValue()).lt(AyFaceTask::getUpdatedTime,LocalDateTime.now().plusSeconds(-1*analyzeProperties.getTaskCanStopTime()))));
        List<Object> canStopTaskIds=this.listObjs(wrapper);
        if(canStopTaskIds.isEmpty()){
            return false;
        }
        ids = (List<Integer>)(List)canStopTaskIds;
        return true;
    }
}
